package com.itheima.controller;

import com.itheima.pojo.Schedule;
import com.itheima.service.ScheduleService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin //允许其他源访问我们的controller
@RestController
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;
    //接收参数
    //响应结果
    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize,
                  @PathVariable int currentPage) {

        R r = scheduleService.page(pageSize,currentPage);
        //sl4fj
        log.info("查询的数据为:{}",r);
        return r;
    }
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id){
        R r = scheduleService.remove(id);
        return  r;
    }

    @PostMapping()
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result){
        if(result.hasErrors()){
            return  R.fail("参数为null,不能保存");
        }
        R r = scheduleService.save(schedule);
        return r;
    }
    @PutMapping
    public R updata(@Validated @RequestBody Schedule schedule, BindingResult result){
        if(result.hasErrors()){
            return  R.fail("参数为null,不能保存");
        }
        R r = scheduleService.update(schedule);
        return r;
    }
}
